#include "settingsdialog.h"
#include "ui_settingsdialog.h"

#include <QDesktopServices>
#include <QUrl>
#include <QDir>
#include <QFileInfo>
#include <QDate>

SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);
    ui->database_lineEdit->setText(qApp->property("database").toString());
    ui->database_toolButton->setVisible(qApp->property("LocalDatabase").toBool());

    ui->date_label->setStyleSheet("QLabel { background-color : white; }");
    QString dateFormatTip("d	The day as a number without a leading zero (1 to 31)\n"
                          "dd	The day as a number with a leading zero (01 to 31)\n"
                          "ddd	The abbreviated localized day name (e.g. 'Mon' to 'Sun').\n"
                          "dddd	The long localized day name (e.g. 'Monday' to 'Sunday').\n"
                          "M	The month as a number without a leading zero (1 to 12)\n"
                          "MM	The month as a number with a leading zero (01 to 12)\n"
                          "MMM	The abbreviated localized month name (e.g. 'Jan' to 'Dec').\n"
                          "MMMM	The long localized month name (e.g. 'January' to 'December').\n"
                          "yy	The year as two digit number (00 to 99)\n"
                          "yyyy	The year as four digit number. If the year is negative, a minus sign is prepended in addition);");
    ui->date_label->setToolTip(tr("Date preview"));
    ui->date_format_lineEdit->setToolTip(dateFormatTip);
    ui->date_format_pushButton->setToolTip(tr("Reset date format"));
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::setAutoFillDate(const bool autofill)
{
    ui->autodate_checkBox->setChecked(autofill);
}

void SettingsDialog::setAutoFillReceive(const bool autofill)
{
    ui->autoreceive_checkBox->setChecked(autofill);
}

void SettingsDialog::setAutoNAFilter(const bool autofilter)
{
    ui->nafilter_checkBox->setChecked(autofilter);
}

void SettingsDialog::setMaxNumOfRows(const int rows)
{
    ui->maxrows_spinBox->setValue(rows);
}

bool SettingsDialog::getAutoFillDate() const
{
    return ui->autodate_checkBox->isChecked();
}

bool SettingsDialog::getAutoFillReceive() const
{
    return ui->autoreceive_checkBox->isChecked();
}

bool SettingsDialog::getAutoNAFilter() const
{
    return ui->nafilter_checkBox->isChecked();
}

int SettingsDialog::getMaxNumOfRows() const
{
    return ui->maxrows_spinBox->value();
}

void SettingsDialog::on_database_toolButton_clicked()
{
    //QDesktopServices::openUrl(QUrl(QFileInfo(qApp->property("database").toString()).path(), QUrl::TolerantMode));

    QUrl url;
    QString filePath = QFileInfo(qApp->property("database").toString()).path();
    if (filePath.startsWith("\\\\") || filePath.startsWith("//"))
        url.setUrl(QDir::toNativeSeparators(filePath));
    else
        url = QUrl::fromLocalFile(filePath);
    if(!QDesktopServices::openUrl(url)){
        //qDebug() << tr("Error with folder opening. The url path:\n%1")
        //            .arg(filePath);
    }
}

void SettingsDialog::on_date_format_lineEdit_textChanged(const QString &arg1)
{
    ui->date_label->setText(QDate::currentDate().toString(arg1));
    ui->date_format_pushButton->setDisabled(
                arg1 == ("dd.MM.yyyy")
                );
}

void SettingsDialog::setDateFormat(const QString &format)
{
    ui->date_format_lineEdit->setText(format);
}

QString SettingsDialog::getDateFormat() const
{
    return ui->date_format_lineEdit->text().trimmed();
}

void SettingsDialog::on_date_format_pushButton_clicked()
{
    ui->date_format_lineEdit->setText("dd.MM.yyyy");
}
